home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP02.ZIP / CHAP02 / PATRON / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  9KB  |  466 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Original Starter Chapter 2
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <memory.h>
  19. #include "patron.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * CPatronDoc::CPatronDoc
  25.  * CPatronDoc::~CPatronDoc
  26.  *
  27.  * Constructor Parameters:
  28.  *  hInst           HINSTANCE of the application.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst)
  32.     : CDocument(hInst)
  33.     {
  34.     m_pPG=NULL;
  35.     m_lVer=VERSIONCURRENT;
  36.     return;
  37.     }
  38.  
  39.  
  40. CPatronDoc::~CPatronDoc(void)
  41.     {
  42.     if (NULL!=m_pPG)
  43.         delete m_pPG;
  44.  
  45.     return;
  46.     }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. /*
  53.  * CPatronDoc::FInit
  54.  *
  55.  * Purpose:
  56.  *  Initializes an already created document window.  The client actually
  57.  *  creates the window for us, then passes that here for further
  58.  *  initialization.
  59.  *
  60.  * Parameters:
  61.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  62.  *
  63.  * Return Value:
  64.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  65.  */
  66.  
  67. BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
  68.     {
  69.     //Change the stringtable range to our customization.
  70.     pDI->idsMin=IDS_DOCUMENTMIN;
  71.     pDI->idsMax=IDS_DOCUMENTMAX;
  72.  
  73.     //Do default initialization
  74.     if (!CDocument::FInit(pDI))
  75.         return FALSE;
  76.  
  77.     //Pages are created when we get a ::ULoad later.
  78.     return TRUE;
  79.     }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  * CPatronDoc::FMessageHook
  88.  *
  89.  * Purpose:
  90.  *  Processes WM_SIZE for the document so we can resize the Pages window.
  91.  *
  92.  * Parameters:
  93.  *  <WndProc Parameters>
  94.  *  pLRes           LRESULT FAR * in which to store the return value
  95.  *                  for the message.
  96.  *
  97.  * Return Value:
  98.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  99.  */
  100.  
  101. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  102.     , LPARAM lParam, LRESULT FAR *pLRes)
  103.     {
  104.     UINT        dx, dy;
  105.     RECT        rc;
  106.  
  107.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  108.         {
  109.         dx=LOWORD(lParam);
  110.         dy=HIWORD(lParam);
  111.  
  112.         //Resize the Pages window to fit the new client area of the document
  113.         GetClientRect(hWnd, &rc);
  114.         m_pPG->RectSet(&rc, FALSE);
  115.         }
  116.  
  117.     /*
  118.      * We return FALSE even on WM_SIZE so we can let the default procedure
  119.      * handle maximized MDI child windows appropriately.
  120.      */
  121.     return FALSE;
  122.     }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /*
  132.  * CPatronDoc::Clear
  133.  *
  134.  * Purpose:
  135.  *  Sets all contents in the document back to defaults with no filename.
  136.  *
  137.  * Paramters:
  138.  *  None
  139.  *
  140.  * Return Value:
  141.  *  None
  142.  */
  143.  
  144. void CPatronDoc::Clear(void)
  145.     {
  146.     //Completely reset the pages
  147.     m_pPG->New();
  148.  
  149.     CDocument::Clear();
  150.     m_lVer=VERSIONCURRENT;
  151.     return;
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. /*
  159.  * CPatronDoc::ULoad
  160.  *
  161.  * Purpose:
  162.  *  Loads a given document without any user interface overwriting the
  163.  *  previous contents of the editor.
  164.  *
  165.  * Parameters:
  166.  *  fChangeFile     BOOL indicating if we're to update the window title
  167.  *                  and the filename from using this file.
  168.  *  pszFile         LPSTR to the filename to load.  Could be NULL for
  169.  *                  an untitled document.
  170.  *
  171.  * Return Value:
  172.  *  UINT            An error value from DOCERR_*
  173.  */
  174.  
  175. UINT CPatronDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  176.     {
  177.     RECT        rc;
  178.  
  179.     //We don't support opening anything yet.
  180.     if (NULL!=pszFile)
  181.         return DOCERR_NONE;
  182.  
  183.     //Attempt to create our contained Pages window.
  184.     m_pPG=new CPages(m_hInst);
  185.     GetClientRect(m_hWnd, &rc);
  186.  
  187.     if (!m_pPG->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_PAGES, NULL))
  188.         return DOCERR_NOFILE;
  189.  
  190.     //Go initialize the Pages for the default printer.
  191.     if (!PrinterSetup(NULL, TRUE))
  192.         return DOCERR_COULDNOTOPEN;
  193.  
  194.     Rename(NULL);
  195.  
  196.     //Go create an initial page.
  197.     m_pPG->PageInsert(0);
  198.  
  199.     FDirtySet(FALSE);
  200.     return DOCERR_NONE;
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. /*
  208.  * CPatronDoc::Print
  209.  *
  210.  * Purpose:
  211.  *  Prints the current document.
  212.  *
  213.  * Parameters:
  214.  *  hWndFrame       HWND of the frame to use for dialog parents.
  215.  *
  216.  * Return Value:
  217.  *  BOOL            TRUE if printing happened, FALSE if it didn't start
  218.  *                  or didn't complete.
  219.  */
  220.  
  221. BOOL CPatronDoc::Print(HWND hWndFrame)
  222.     {
  223.     PRINTDLG        pd;
  224.     BOOL            fSuccess;
  225.  
  226.     memset(&pd, 0, sizeof(PRINTDLG));
  227.     pd.lStructSize=sizeof(PRINTDLG);
  228.     pd.hwndOwner  =hWndFrame;
  229.     pd.nCopies    =1;
  230.     pd.nFromPage  =-1;
  231.     pd.nToPage    =-1;
  232.     pd.nMinPage   =1;
  233.     pd.nMaxPage   =m_pPG->NumPagesGet();
  234.  
  235.     //Get the current document printer settings
  236.     pd.hDevMode=m_pPG->DevModeGet();
  237.  
  238.     pd.Flags=PD_RETURNDC | PD_ALLPAGES | PD_COLLATE
  239.         | PD_HIDEPRINTTOFILE | PD_NOSELECTION;
  240.  
  241.     if (!PrintDlg(&pd))
  242.         return FALSE;
  243.  
  244.     //Make sure the Pages knows about any printer changes.
  245.     if (!m_pPG->DevModeSet(pd.hDevMode, pd.hDevNames))
  246.         {
  247.         GlobalFree(pd.hDevMode);
  248.         GlobalFree(pd.hDevNames);
  249.         return FALSE;
  250.         }
  251.  
  252.     //Go do the actual printing.
  253.     fSuccess=m_pPG->Print(pd.hDC, PSZ(IDS_DOCUMENTNAME), pd.Flags
  254.         , pd.nFromPage, pd.nToPage, pd.nCopies);
  255.  
  256.     if (!fSuccess)
  257.         MessageBox(m_hWnd, PSZ(IDS_PRINTERROR), PSZ(IDS_DOCUMENTCAPTION), MB_OK);
  258.  
  259.     return fSuccess;
  260.     }
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. /*
  268.  * CPatronDoc::PrinterSetup
  269.  *
  270.  * Purpose:
  271.  *  Selects a new printer and options for this document.
  272.  *
  273.  * Parameters:
  274.  *  hWndFrame       HWND of the frame to use for dialog parents.
  275.  *  fDefault        BOOL to avoid any dialog and just use the default.
  276.  *
  277.  * Return Value:
  278.  *  UINT            Undefined
  279.  *
  280.  */
  281.  
  282. UINT CPatronDoc::PrinterSetup(HWND hWndFrame, BOOL fDefault)
  283.     {
  284.     PRINTDLG        pd;
  285.  
  286.     //Attempt to get printer metrics for the default printer.
  287.     memset(&pd, 0, sizeof(PRINTDLG));
  288.     pd.lStructSize=sizeof(PRINTDLG);
  289.  
  290.     if (fDefault)
  291.         pd.Flags=PD_RETURNDEFAULT;
  292.     else
  293.         {
  294.         pd.hwndOwner=hWndFrame;
  295.         pd.Flags=PD_PRINTSETUP;
  296.  
  297.         //Get the current document printer settings
  298.         pd.hDevMode=m_pPG->DevModeGet();
  299.         }
  300.  
  301.     if (!PrintDlg(&pd))
  302.         return FALSE;
  303.  
  304.     if (!m_pPG->DevModeSet(pd.hDevMode, pd.hDevNames))
  305.         {
  306.         GlobalFree(pd.hDevNames);
  307.         GlobalFree(pd.hDevMode);
  308.         return FALSE;
  309.         }
  310.  
  311.     FDirtySet(TRUE);
  312.     return 1;
  313.     }
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321. /*
  322.  * CPatronDoc::NewPage
  323.  *
  324.  * Purpose:
  325.  *  Creates a new page in the document's pages control after the
  326.  *  current page.
  327.  *
  328.  * Parameters:
  329.  *  None
  330.  *
  331.  * Return Value:
  332.  *  UINT            Index of the new page.
  333.  */
  334.  
  335. UINT CPatronDoc::NewPage(void)
  336.     {
  337.     FDirtySet(TRUE);
  338.     return m_pPG->PageInsert(0);
  339.     }
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347. /*
  348.  * CPatronDoc::DeletePage
  349.  *
  350.  * Purpose:
  351.  *  Deletes the current page from the document.
  352.  *
  353.  * Parameters:
  354.  *  None
  355.  *
  356.  * Return Value:
  357.  *  UINT            Index of the now current page.
  358.  */
  359.  
  360. UINT CPatronDoc::DeletePage(void)
  361.     {
  362.     FDirtySet(TRUE);
  363.     return m_pPG->PageDelete(0);
  364.     }
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372. /*
  373.  * CPatronDoc::NextPage
  374.  *
  375.  * Purpose:
  376.  *  Shows the next page in the pages window.
  377.  *
  378.  * Parameters:
  379.  *  None
  380.  *
  381.  * Return Value:
  382.  *  UINT            Index of the new page.
  383.  */
  384.  
  385. UINT CPatronDoc::NextPage(void)
  386.     {
  387.     UINT        iPage;
  388.  
  389.     iPage=m_pPG->CurPageGet();
  390.     return m_pPG->CurPageSet(++iPage);
  391.     }
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399. /*
  400.  * CPatronDoc::PreviousPage
  401.  *
  402.  * Purpose:
  403.  *  Shows the previous page in the pages window.
  404.  *
  405.  * Parameters:
  406.  *  None
  407.  *
  408.  * Return Value:
  409.  *  UINT            Index of the new page.
  410.  */
  411.  
  412. UINT CPatronDoc::PreviousPage(void)
  413.     {
  414.     UINT        iPage;
  415.  
  416.     //If iPage is zero, then we wrap around to the end.
  417.     iPage=m_pPG->CurPageGet();
  418.     return m_pPG->CurPageSet(--iPage);
  419.     }
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426. /*
  427.  * CPatronDoc::First